// ========================================================
// Des：
// Creator：DD 
// Time：2021-01-21 12:02:30
// Version：1.0
// ========================================================

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class XorEncyptStream : FileStream
{
    private const byte KEY = 29;

    public XorEncyptStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) : base(path, mode, access, share, bufferSize, useAsync)
    {
    }

    public XorEncyptStream(string path, FileMode mode) : base(path, mode)
    {
    }

    public override int Read(byte[] array, int offset, int count)
    {
        var index = base.Read(array, offset, count);
        for (int i = 0; i < array.Length; i++)
        {
            array[i] ^= KEY;
        }
        return index;
    }

    public override void Write(byte[] array, int offset, int count)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] ^= KEY;
        }
        base.Write(array, offset, count);
    }
}

public class OffsetEncyptStream : FileStream
{
    public OffsetEncyptStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) : base(path, mode, access, share, bufferSize, useAsync)
    {
    }

    public OffsetEncyptStream(string path, FileMode mode) : base(path, mode)
    {
    }

    public override void Write(byte[] array, int offset, int count)
    {
        byte[] offsetBytes = new byte[13];
        System.Random random = new System.Random();
        random.NextBytes(offsetBytes);
        byte[] encyptBytes = new byte[count + offsetBytes.Length];
        System.Buffer.BlockCopy(offsetBytes, 0, encyptBytes, 0, offsetBytes.Length);
        System.Buffer.BlockCopy(array, offset, encyptBytes, offsetBytes.Length, count);
        base.Write(encyptBytes, 0, encyptBytes.Length);
    }
}
